package com.pinc.exam;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;

public class Group_Content extends WidgetActivity {
	private ListView lv;
	private DBClass dbHelper;
	private Cursor mCursor;
	private Question_Adapter mAdapter;
	private long mId;
	private int mMode;
	private int mSortBy;
	private long mDelQid;
	private long mDelQpos;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_layout);

		mId = this.getIntent().getLongExtra("id", 0);
		mMode = Question_Adapter.MODE_NORMAL;
		dbHelper = new DBClass(this);

		mSortBy = 0;
		initWidget();
	}

	@Override
	protected void onResume() {
		super.onResume();
		showQuestions();
	}

	private void showQuestions()
	{
		mAdapter = new Question_Adapter(this, mMode);

		mCursor = dbHelper.getQuestionsInGroup(mId);

		if (mCursor.moveToFirst())
		{
			do{
				Question_Class qc = new Question_Class(
						mCursor.getInt(mCursor.getColumnIndex("_id")),
						mCursor.getInt(mCursor.getColumnIndex(DBClass.COL_GROUP_QID)),
						mCursor.getString(mCursor.getColumnIndex(DBClass.COL_QLIB_QUESTION)),
						mCursor.getInt(mCursor.getColumnIndex(DBClass.COL_QLIB_BINGO_NO)),
						mCursor.getInt(mCursor.getColumnIndex(DBClass.COL_QLIB_TESTED_NO)),
						mCursor.getFloat(mCursor.getColumnIndex(DBClass.COL_QLIB_CREDIT))
				);
				mAdapter.add(qc);
			}while(mCursor.moveToNext());
		}
		mCursor.close();
		dbHelper.mDB.close();

		mAdapter.sort(mSortBy);
		lv.setAdapter(mAdapter);
	}

	@Override
	protected void initWidget() {
		super.initWidget();

		lv = (ListView) this.findViewById(R.id.listView1);

		/* set upbar */
		tv_title.setText(dbHelper.getGroupName(mId));
		NormalMode();

	}

	private static final int ID_DEL_CONFIRM = 0;
	private static final int ID_RENAME = 1;
	private static final int ID_NAME_LEN = 2;
	private static final int ID_DEL_DELQ = 3;
	@Override
	protected Dialog onCreateDialog(int id) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);

		switch(id)
		{
		case ID_DEL_CONFIRM:
		{
			builder.setMessage(getResources().getString(R.string.str_confirm_del_group))
			.setPositiveButton(getResources().getString(R.string.str_delete), new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dbHelper.deleteGroup(mId);
					finish();
				}
			})
			.setNegativeButton(getResources().getString(R.string.str_cancel), null);
			break;
		}
		case ID_RENAME:
		{
			LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			final EditText input = (EditText) inflater.inflate(R.layout.edittext_group_name, null);

			builder.setMessage(getResources().getString(R.string.str_input_new_name))
			.setView(input)
			.setPositiveButton(getResources().getString(R.string.str_ok), new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					if (input.getText().length() < 1)
						showDialog(ID_NAME_LEN);
					else
					{
						dbHelper.renameGroup(mId, input.getText().toString());
						tv_title.setText(dbHelper.getGroupName(mId));
					}
					input.setText("");
				}
			})
			.setNegativeButton(getResources().getString(R.string.str_cancel), null);
			break;
		}
		case ID_NAME_LEN:
		{
			builder.setMessage(getResources().getString(R.string.str_invalid_name))
			.setPositiveButton(getResources().getString(R.string.str_ok), null)
			.setCancelable(false);
			break;
		}
		case ID_DEL_DELQ:
		{
			builder.setMessage("")
			.setPositiveButton(getResources().getString(R.string.str_delete), new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dbHelper.delQuestionInGroup(mDelQid, mId);
					showQuestions();
				}
			})
			.setNegativeButton(getResources().getString(R.string.str_cancel), null);
			break;
		}
		default:
			break;
		}
		return builder.create();
	}


	@Override
	protected void onPrepareDialog(int id, Dialog dialog) {
		switch (id)
		{
		case ID_DEL_DELQ:
			((AlertDialog)dialog).setMessage(getResources().getString(R.string.str_confirm_del_question_prefix) + (mDelQpos+1) + getResources().getString(R.string.str_confirm_del_question_suffix));
			break;
		default:
			break;
		}
		super.onPrepareDialog(id, dialog);
	}

	private void EditMode()
	{
		b_prev.setVisibility(View.VISIBLE);
		b_prev.setText(getResources().getString(R.string.str_finish));
		b_prev.setOnClickListener(EditDone);

		b_next.setText(getResources().getString(R.string.str_add));
		b_next.setOnClickListener(AddQuestion);

		lv.setOnItemClickListener(DeleteListener);

		mMode = Question_Adapter.MODE_DELETE;
	}

	private void NormalMode()
	{
		b_prev.setVisibility(View.VISIBLE);
		b_prev.setText(getResources().getString(R.string.str_back));
		b_prev.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});

		b_next.setVisibility(View.VISIBLE);
		b_next.setText(getResources().getString(R.string.str_menu));
		b_next.setOnClickListener(ShowMenu);

		lv.setOnItemClickListener(ViewListener);

		mMode = Question_Adapter.MODE_NORMAL;
	}

	private void updateAdapterMode()
	{
		mAdapter.setMode(mMode);
		lv.setAdapter(mAdapter);
	}

	private OnItemClickListener ViewListener = new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,	long id) {
			Intent intent = new Intent(Group_Content.this, Question_Content.class);
			intent.putExtra("title", dbHelper.getGroupName(mId));
			intent.putExtra("ids", mAdapter.getIdArray());
			intent.putExtra("pos", position);
			startActivity(intent);
		}
	};

	private View.OnClickListener EditDone = new View.OnClickListener(){
		@Override
		public void onClick(View v) {
			NormalMode();
			updateAdapterMode();
		}
	};

	private View.OnClickListener ShowMenu = new View.OnClickListener(){
		@Override
		public void onClick(View v) {
			Group_Content.this.openOptionsMenu();
		}
	};

	private View.OnClickListener AddQuestion = new View.OnClickListener(){
		@Override
		public void onClick(View v) {
			Intent intent = new Intent(Group_Content.this, Category_View.class);
			intent.putExtra("mode", Category_View.MODE_ADD);
			intent.putExtra("group_id", mId);
			startActivity(intent);
		}
	};

	private OnItemClickListener DeleteListener = new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,	long id) {
			mDelQid = id;
			mDelQpos = position;
			showDialog(ID_DEL_DELQ);
		}
	};

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.group, menu);
		return true;
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		if ( mMode == Question_Adapter.MODE_NORMAL)
			return super.onPrepareOptionsMenu(menu);
		else
			return false;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.sortby_rate:
			mAdapter.sort(mSortBy = 1);
			lv.setAdapter(mAdapter);
			return true;
		case R.id.sortby_id:
			mAdapter.sort(mSortBy = 0);
			lv.setAdapter(mAdapter);
			return true;
		case R.id.edit:
			EditMode();
			updateAdapterMode();
			return true;
		case R.id.rename:
			showDialog(ID_RENAME);
			return true;
		case R.id.delete:
			showDialog(ID_DEL_CONFIRM);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
}
